//
//  SEGMyManager.m
//  ExcellentHome
//
//  Created by 刘山 on 16/11/19.
//  Copyright © 2016年 陈区. All rights reserved.
//

#import "SEGMyManager.h"

@interface SEGMyManager ()



@end

@implementation SEGMyManager

/**
 * 用户保存线上实物领奖地址
 * @author Wendy
 * @param finished 回调结果
 */
- (void)requestUpdateAddrByWinnerId:(NSString *)winnerId
                         reciveName:(NSString *)reciveName
                          reciveTel:(NSString *)reciveTel
                         reciveAddr:(NSString *)reciveAddr
                           finished:(SEGNetworkReponse)finished
{
    NSDictionary* params = @{
                             @"userId" : kAccountUserID,
                             @"winnerId" : winnerId,
                             @"reciveName" : reciveName,
                             @"reciveTel" : reciveTel,
                             @"reciveAddr" : reciveAddr
                             };
        NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/act-api/actvityBehavior/updateAddr.json",nil];

    [SEGHTTPBusinessManager seg_requestJSONWithMethod:POST
                                            URLString:urlString
                                           parameters:params
                                             finished:^(BOOL isSuccessed,
                                                        id result,
                                                        NSString *codeType,
                                                        NSString *errorMessage) {
                                                 
                                                 finished(isSuccessed,result, codeType, errorMessage);
                                             }];
}

//获取我的头像文件名称
+ (NSString*)myPortraitFileNameWithUserID:(NSString*)userID
{
    NSString* fileName = [NSString stringWithFormat:@"MyPortrait_%@.png", userID];
    return fileName;
}

//根据文件名获取媒体文件完整路径
+ (NSString*)mideaFilePathForFileName:(NSString*)aFileName
{
    if (!aFileName) {
        return nil;
    }
    return [[SEGMyManager mideaFilePath] stringByAppendingPathComponent:aFileName];
}

//获取媒体文件存放路径
+ (NSString*)mideaFilePath
{
    return [[SEGMyManager documentPath] stringByAppendingPathComponent:@"midea"];
}

//获取document路径
+ (NSString*)documentPath
{
    //获取存档对象的路径
    NSArray* paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString* documentPath = [paths objectAtIndex:0];
    return documentPath;
}

#pragma mark - 接口请求
///  6.获取个人配送地址
- (void)requestMyAddressFinished:(SEGNetworkReponse)finished
{
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-sso/v1/userApp/info/getMyAddress",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:nil finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(NO,result,codeType,errorMessage);
            return ;
        }
        
        NSArray* arr = [result valueForKey:@"data"];
        
        if (arr && arr.count <= 0) {
            self.seg_segaddressmodel = [NSArray array];
            finished(NO,result, codeType, @"无配送地址"); //数据返回正常，但是数据为空
            return;
        }
        
        NSMutableArray* arrM = [NSMutableArray arrayWithCapacity:arr.count];
        
        for (NSDictionary* dic in arr) {
            
            SEGAddressModel* _model = [SEGAddressModel initWithDictionary:dic];
            
            [arrM addObject:_model];
        }
        
        self.seg_segaddressmodel = arrM;
        
        finished(YES,result, codeType, errorMessage);
    }];
}

///  4.设置默认配送地址
- (void)requestSetDefaultAddressByid:(NSString*)AddressId Finished:(SEGNetworkReponse)finished
{
    
    NSDictionary* params = @{
                             @"id" : AddressId
                             };
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-sso/v1/userApp/info/setDefaultAddress",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(NO,result,codeType,errorMessage);
            return ;
        }
        
        finished(YES,result,codeType,errorMessage);
    }];
}

///  7.添加个人配送地址
- (void)requestaddAddressByAddress:(NSString*)address Finished:(SEGNetworkReponse)finished
{
    
    NSDictionary* params = @{
                             @"address" : address
                             };
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-sso/v1/userApp/info/addAddress",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(NO,result,codeType,errorMessage);
            return ;
        }
        
        finished(YES,result,codeType,errorMessage);
    }];
}

///  9.删除个人配送地址
- (void)requestDeleteAddressByid:(NSString*)AddressId Finished:(SEGNetworkReponse)finished
{
    
    NSDictionary* params = @{
                             @"id" : AddressId
                             };
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-sso/v1/userApp/info/deleteAddress",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(NO,result,codeType,errorMessage);
            return ;
        }
        
        finished(YES,result,codeType,errorMessage);
    }];
}

///  8.新版-添加个人配送地址
- (void)requestNewAddAddressByaddress:(NSString*)address
                          contactName:(NSString*)contactName
                         contactPhone:(NSString*)contactPhone
                            isDefault:(NSString*)isDefault
                             Finished:(SEGNetworkReponse)finished

{
    
    NSDictionary* params = @{
                             @"address" : address,
                             @"contactName" : contactName,
                             @"contactPhone" : contactPhone,
                             @"isDefault" : isDefault
                             };
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-sso/v1/userApp/info/newAddAddress",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(NO,result,codeType,errorMessage);
            return ;
        }
        NSDictionary* dic = [result valueForKey:@"data"];
        NSString* message1 = [dic valueForKey:@"id"];
        finished(YES,result, message1, errorMessage);
    }];
}

///  3.修改个人配送地址
- (void)requestUpdateAddressByid:(NSString*)AddressId
                         address:(NSString*)address
                     contactName:(NSString*)contactName
                    contactPhone:(NSString*)contactPhone
                       isDefault:(NSString*)isDefault
                        Finished:(SEGNetworkReponse)finished
{
    
    NSDictionary* params = @{
                             @"id" : AddressId,
                             @"address" : address,
                             @"contactName" : contactName,
                             @"contactPhone" : contactPhone,
                             @"isDefault" : isDefault
                             };
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-sso/v1/userApp/info/updateAddAddress",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(NO,result,codeType,errorMessage);
            return ;
        }
        
        finished(YES,result,codeType,errorMessage);
    }];
}

/// 获取职业列表
- (void)requestUserJobMessageFinished:(SEGNetworkReponse)finished{
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/bms-api/base/profession/list",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:nil finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(NO,result,codeType,errorMessage);
            return ;
        }
        
        NSMutableArray *arrM = [NSMutableArray array];
        NSArray* arrList = [result valueForKey:@"data"];
        
        for (NSDictionary *dic in arrList) {
            SEGJobMessageModel *model = [SEGJobMessageModel initWithDictionary:dic];
            [arrM addObject:model];
        }
        
        self.seg_segjobmessagemodel = arrM;
        finished(YES,result,codeType,errorMessage);
    }];
}

- (void)requestUpdateProperyInfoBynickname:(NSString*)nickname
                                       ico:(NSString*)ico
                                      name:(NSString*)name
                                       age:(NSString*)age
                                       sex:(NSString*)sex
                                       job:(NSString*)job
                                      desc:(NSString*)desc
                                  identity:(NSString*)identity
                                  Finished:(SEGNetworkReponse)finished
{
    
    NSDictionary* params = @{
                             @"nickname" : ValueToString(nickname),
                             @"ico" : ValueToString(ico),
                             @"name" : ValueToString(name),
                             @"sex" : ValueToString(sex),
                             @"job" : ValueToString(job),
                             @"desc" : ValueToString(desc),
                             @"identity" : ValueToString(identity),
                             @"age" : ValueToString(age)
                             };
    
    NSMutableDictionary* dict = [NSMutableDictionary dictionaryWithDictionary:params];
    
    
    if (!nickname || [nickname isEqualToString:@""]) {
        [dict removeObjectForKey:@"nickname"];
    }
    if (!ico || [ico isEqualToString:@""]) {
        [dict removeObjectForKey:@"ico"];
    }
    if (!name || [name isEqualToString:@""]) {
        [dict removeObjectForKey:@"name"];
    }
    if (!sex || [sex isEqualToString:@""]) {
        [dict removeObjectForKey:@"sex"];
    }
    if (!job || [job isEqualToString:@""]) {
        [dict removeObjectForKey:@"job"];
    }
    if (!desc) { 
        [dict removeObjectForKey:@"desc"];
    }
    if (!identity || [identity isEqualToString:@""]) {
        [dict removeObjectForKey:@"identity"];
    }
    if (!age || [age isEqualToString:@""]) {
        [dict removeObjectForKey:@"age"];
    }
    
    params = dict;
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-sso/v1/userApp/info/updateUserInfo",nil];

    [SEGHTTPBusinessManager seg_requestWithMethod:POST URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(NO,result,codeType,errorMessage);
            return ;
        }
        
        finished(YES,result,codeType,errorMessage);
    }];
}

//根据用户ID,获取用户信息
-(void)requestGetUserInfoByUserId:(NSString *)userId Finished:(SEGNetworkReponse)finished{
    
    NSDictionary* params = @{
                             @"userId" : userId
                             };
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/uhomecp-sso/v3/userApp/getUserInfo",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:params finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            //失败判断是自己还是别人，自己用缓存，别人缺省图
            finished(NO,result,codeType,errorMessage);
            return ;
        }
        

        
        finished(YES,result,codeType,errorMessage);
    }];
}






//个人主页 新版- 我发布的UGC
-(void)requestNewQueryUGCByUserId:(NSString *)userId
                           pageNo:(NSString *)pageNo
                        pageLimit:(NSString *)pageLimit
                         Finished:(SEGNetworkReponse)finished{
    
    NSDictionary* params = @{
                             @"userId" : userId,
                             @"pageNo" : pageNo,
                             @"pageLimit" : pageLimit
                             };
    
    NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/cms-api/api/v2/queryUGCByUserId",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET
                                        URLString:urlString
                                       parameters:params
                                         finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
                                             
                                             if (!isSuccessed) {
                                                 finished(NO,result,codeType,errorMessage);
                                                 return ;
                                             }
                                             
                                             NSDictionary* dicData = [result valueForKey:@"data"];
                                             if ((!dicData) || (![dicData isKindOfClass:[NSDictionary class]])) {
                                                 finished(NO,result, codeType, errorMessage);
                                                 return;
                                             }
                                             
                                             NSArray* list = [dicData valueForKey:@"ugcList"];
                                             
                                             if (!list || ![list isKindOfClass:[NSArray class]]) {
                                                 finished(NO,result, codeType, errorMessage);
                                                 return;
                                             }
                                             
                                             if ([pageNo integerValue] != 1 && [list isKindOfClass:[NSArray class]] && list.count == 0) {
                                                 finished(NO,result, codeType, @"");
                                                 return;
                                             }
                                             
                                             if ([pageNo integerValue] == 1) { //顶部刷新
//                                                 self.layout = [NSMutableArray array];
                                                 self.originDataArray = [NSMutableArray array];
                                             }
        
                                             // 保存原始数据
                                             [self.originDataArray addObjectsFromArray:list];
                                             
//                                             //布局传入数据源
//                                             [self segAllLayoutWithDataList:list];
//
//                                             //遍历年，存储年
//                                             [self.layout enumerateObjectsUsingBlock:^(SEGCustomWholeViewLayout * _Nonnull obj1, NSUInteger idx1, BOOL * _Nonnull stop1) {
//                                                 __block BOOL isContain = NO;
//                                                 [self.yearsStringArray enumerateObjectsUsingBlock:^(id  _Nonnull obj2, NSUInteger idx, BOOL * _Nonnull stop) {
//                                                     if ([obj1.data.year isEqualToString:obj2]) {
//                                                         isContain = YES;
//                                                         *stop = YES;
//                                                     }
//                                                 }];
//                                                 if (!isContain) {
//                                                     [self.yearsStringArray addObject:obj1.data.year];
//                                                 }
//                                             }];
//
//                                             //按年份遍历，获取年份及下所有月份的数据源
//                                             NSMutableArray<NSMutableArray<SEGCustomWholeViewLayout*>*> *data = [NSMutableArray array];
//                                             [self.yearsStringArray enumerateObjectsUsingBlock:^(NSString*  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
//                                                 NSMutableArray<SEGCustomWholeViewLayout*> *arrM = [NSMutableArray array];
//                                                 [self.layout enumerateObjectsUsingBlock:^(SEGCustomWholeViewLayout * _Nonnull obj1, NSUInteger idx1, BOOL * _Nonnull stop1) {
//                                                     if ([obj isEqualToString:obj1.data.year]) {
//                                                         [arrM addObject:obj1];
//                                                     }
//                                                 }];
//                                                 [data addObject:arrM];
//                                                 self.dataSource = data;
//                                             }];
                                             
                                             finished(YES,result,codeType,errorMessage);
                                         }];
    
}







//获取积分
-(void)requestgetCompleteInformationFinished:(SEGNetworkReponse)finished{
        NSString *urlString = [SEGServerManager serverUrlJoint:kClientRootURL,@"/bms-api/message/getCompleteInformation",nil];
    [SEGHTTPBusinessManager seg_requestWithMethod:GET URLString:urlString parameters:nil finished:^(BOOL isSuccessed, id result, NSString *codeType, NSString *errorMessage) {
        if (!isSuccessed) {
            finished(NO,result, codeType, errorMessage);
            return;
        }
        
        if ([result valueForKey:@"data"] &&
            [[result valueForKey:@"data"] isKindOfClass:[NSDictionary class]]) {
            self.seg_segcompleteinfomodel = [SEGPersonalCompleteInfoModel initWithDictionary:[result valueForKey:@"data"]];
            finished(YES,result, codeType, errorMessage);
            return;
        }
        
        finished(isSuccessed,result, codeType, errorMessage);
    }];
}


#pragma mark - getter
- (NSArray<SEGAddressModel*>*)seg_segaddressmodel
{
    if (!_seg_segaddressmodel) {
        _seg_segaddressmodel = [NSArray array];
    }
    return _seg_segaddressmodel;
}

- (SEGDictionaryInfoModel*)seg_segdictionaryinfomodel
{
    if (!_seg_segdictionaryinfomodel) {
        _seg_segdictionaryinfomodel = [[SEGDictionaryInfoModel alloc] init];
    }
    return _seg_segdictionaryinfomodel;
}

- (NSArray<SEGJobMessageModel*>*)seg_segjobmessagemodel
{
    if (!_seg_segjobmessagemodel)
    {
        _seg_segjobmessagemodel = [NSArray array];
    }
    return _seg_segjobmessagemodel;
}



-(NSMutableArray *)yearsStringArray{
    if (!_yearsStringArray) {
        _yearsStringArray = [NSMutableArray array];
    }
    return _yearsStringArray;
}

-(SEGPersonalCompleteInfoModel *)seg_segcompleteinfomodel{
    if (!_seg_segcompleteinfomodel) {
        _seg_segcompleteinfomodel = [SEGPersonalCompleteInfoModel new];
    }
    return _seg_segcompleteinfomodel;
}

- (NSMutableArray *)originDataArray {
    if (!_originDataArray) {
        _originDataArray = [NSMutableArray array];
    }
    return _originDataArray;
}

@end
